package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.mapper.ReimbursementOrdersInfoMapper;
import com.ytjj.qmyx.admin.mapper.ReimbursementOrdersMapper;
import com.ytjj.qmyx.admin.model.ReimbursementOrders;
import com.ytjj.qmyx.admin.model.ReimbursementOrdersInfo;
import com.ytjj.qmyx.admin.model.request.ReiOrderRequest;
import com.ytjj.qmyx.admin.model.request.ReimbursementOrderRequest;
import com.ytjj.qmyx.admin.model.response.ReiOrderQueryResponse;
import com.ytjj.qmyx.admin.model.response.ReiOrderResponse;
import com.ytjj.qmyx.admin.model.response.ReimbursementExportRecordResponse;
import com.ytjj.qmyx.admin.service.ReimbursementOrdersService;
import com.ytjj.qmyx.mall.model.Banker;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 报销管理相关的接口
 * @author jiawei
 * @date 2023年11月13日 10:29
 */
@RestController
@RequestMapping("/reimbursementOrders")
@Slf4j
public class ReimbursementOrdersController {

    @Autowired
    ReimbursementOrdersService reimbursementOrdersService;
    @Autowired
    ReimbursementOrdersMapper reimbursementOrdersMapper;
    @Autowired
    ReimbursementOrdersInfoMapper reimbursementOrdersInfoMapper;
    @Autowired
    private ResourceLoader resourceLoader;
    @Value(value = "${orderPath:}")
    private String orderPath;


    /**
     * 分页查询报销单
     */
    @PostMapping("/searchReimbursementOrders")
    public CommonResult<CommonPage<ReiOrderQueryResponse>> searchReimbursementOrders(@RequestBody ReimbursementOrderRequest request){
        CommonPage<ReiOrderQueryResponse> reimbursementOrders = reimbursementOrdersService.getReimbursementOrders(request);
        return CommonResult.success(reimbursementOrders);
    }

    /**
     * 导出报销单
     */
    @PostMapping("/exportReimbursementOrders")
    @RequiresPermissions("reimbursement:export")
    public CommonResult exportReimbursementOrders(@RequestBody ReimbursementOrderRequest request) {
        reimbursementOrdersService.exportReimbursementOrders(request);
        return CommonResult.success("已执行！");
    }

    /**
     * 下载导出的报销单记录
     * @param id 唯一id
     * @return CommonResult
     */
    @GetMapping("/downloadExcel")
    @RequiresPermissions("reimbursement:export")
    public void downloadExcel(HttpServletResponse response, Integer id) {
        reimbursementOrdersService.downloadExcel(response, id);
    }

    /**
     * 所有供应商
     * @param bankerName 供应商名称，非必填
     * @return CommonResult
     */
    @GetMapping("/getSupplyBanker")
    @RequiresPermissions("reimbursement:getBankerByName")
    public CommonResult<List<Banker>> getSupplyBanker(String bankerName) {
        List<Banker> bankerInfo = reimbursementOrdersService.getBankerByName(bankerName);
        return CommonResult.success(bankerInfo);
    }


    /**
     * 获取导出记录
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getExportRecord")
    public CommonResult<CommonPage<ReimbursementExportRecordResponse>> getExportRecord(Integer pageNum,Integer pageSize){
        CommonPage<ReimbursementExportRecordResponse> exportRecord = reimbursementOrdersService.getExportRecord(pageNum, pageSize, null);
        return CommonResult.success(exportRecord);
    }

    /**
     * 新增或者修改报销单
     */
    @PostMapping("/createOrUpdateReiOrder")
    public CommonResult<Integer> createOrUpdateReiOrder(@RequestBody @Validated ReiOrderRequest request) {
        Integer reiOrder = reimbursementOrdersService.createOrUpdateReiOrder(request);
        return reiOrder > 0 ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 校验子订单是否已报销
     * @param childOrderNos 子订单
     * @param id
     * @return CommonResult<String>
     */
    @GetMapping("/checkReiOrderInfo")
    public CommonResult<String> checkReiOrderInfo(String childOrderNos,String id) {
        String s = reimbursementOrdersService.checkReiOrderInfo(childOrderNos,id);
        return CommonResult.success(s);
    }

    /**
     * 审批或报销
     */
    @RequiresPermissions("reimbursement:reimburse")
    @OperLog(operModule = "报销管理", operDesc = "审批或报销", operType = SysLogConstants.OPERATION_UPDATE)
    @PostMapping("/approvalOrReimburse")
    public CommonResult<Integer> approvalOrReimburse(@RequestBody ReiOrderRequest request) {
        Integer reiOrder = reimbursementOrdersService.approvalOrReimburse(request);
        return reiOrder > 0 ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 查看报销单详情
     */
    @GetMapping("/getReiOrderDetail")
    public CommonResult<ReiOrderResponse> getReiOrderDetail(Integer id) {
        ReiOrderResponse reiOrderDetail = reimbursementOrdersService.getReiOrderDetail(id);
        return CommonResult.success(reiOrderDetail);
    }

    /**
     * 批量删除报销单
     */
    @PostMapping("/delReimbursementOrders")
    public CommonResult<ReiOrderResponse> delReimbursementOrders(@RequestBody List<Integer> id) {
        String result = reimbursementOrdersService.delReimbursementOrders(id);
        return StringUtils.isEmpty(result) ? CommonResult.success() : CommonResult.failed(result);
    }


    /**
     * 获取子订单的报销历史
     */
    @GetMapping("/getReimbursementHistory")
    public CommonResult<List<ReiOrderResponse>> getReimbursementHistory(String childOrderNo) {
        return CommonResult.success(reimbursementOrdersService.getReimbursementHistory(childOrderNo));
    }

    /**
     * 解析导入的excel的报销
     */
    @GetMapping("/analysisReimbursementExcel")
    public CommonResult analysisReimbursementExcel() {
        return CommonResult.success(reimbursementOrdersService.analysisReimbursementExcel());
    }


    /**
     * 手动插入数据
     */
    @GetMapping("/doInsert")
    public CommonResult doInsert() {
        ReimbursementOrders reimbursementOrders = reimbursementOrdersMapper.selectByPrimaryKey(41);
        reimbursementOrders.setId(null);
        for (int i = 0; i < 200; i++) {
            reimbursementOrdersMapper.insert(reimbursementOrders);
            ReimbursementOrdersInfo reimbursementOrdersInfo = reimbursementOrdersInfoMapper.selectByPrimaryKey(78);
            reimbursementOrdersInfo.setId(null);
            reimbursementOrdersInfo.setReimbursementId(reimbursementOrders.getId());
            reimbursementOrdersInfoMapper.insert(reimbursementOrdersInfo);
        }
        return CommonResult.success();
    }


    /**
     * 标记审核状态
     * @param id 唯一标识
     * @param mark 操作标识 0-取消,1-审核,2-确认
     */
    @GetMapping("/markAuditStatus")
    public CommonResult markAuditStatus(Integer id,Integer mark) {
        String error = reimbursementOrdersService.markAuditStatus(id, mark);
        return StringUtils.isEmpty(error) ? CommonResult.success() : CommonResult.failed(error);
    }


}
